package com.eedi.framework.permission.dal.mapper;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.eedi.framework.mybatis.core.mapper.BaseMapperX;
import com.eedi.framework.permission.dal.dataobject.SysUserRoleDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;
import java.util.List;

@Mapper
public interface SysUserRoleMapper extends BaseMapperX<SysUserRoleDO> {

    default List<SysUserRoleDO> selectListByUserId(String userId) {
        return selectList(SysUserRoleDO::getSysUserId, userId);
    }

    default void deleteListByUserIdAndRoleIdIds(String userId, Collection<String> roleIds) {
        delete(new LambdaQueryWrapper<SysUserRoleDO>()
                .eq(SysUserRoleDO::getSysUserId, userId)
                .in(SysUserRoleDO::getSysRoleId, roleIds));
    }

    default void deleteListByUserId(String userId) {
        delete(new LambdaQueryWrapper<SysUserRoleDO>().eq(SysUserRoleDO::getSysUserId, userId));
    }

    default void deleteListByRoleId(String roleId) {
        delete(new LambdaQueryWrapper<SysUserRoleDO>().eq(SysUserRoleDO::getSysRoleId, roleId));
    }

    default List<SysUserRoleDO> selectListByRoleIds(Collection<String> roleIds) {
        if (CollUtil.isEmpty(roleIds)){
            return List.of();
        }
        return selectList(SysUserRoleDO::getSysRoleId, roleIds);
    }

    default List<String> getBySysUserId(String sysUserId) {
        return selectListByUserId(sysUserId).stream().map(SysUserRoleDO::getSysRoleId).toList();
    }

    default Long countByRoleIds(Collection<String> sysRoleIds){
        if (CollUtil.isEmpty(sysRoleIds)){
            return 0L;
        }
       return selectCount(new LambdaQueryWrapper<SysUserRoleDO>()
                .in(SysUserRoleDO::getSysRoleId, sysRoleIds));
    }

    default List<String> getBySysRoleId(Collection<String> sysRoleIds) {
        return  selectList(
                new LambdaQueryWrapper<SysUserRoleDO>().in(SysUserRoleDO::getSysRoleId,sysRoleIds)
        ).stream().map(SysUserRoleDO::getSysUserId).toList();

    }
}
